import { ConverterBase } from './base';
import { ProgressBar, Sprite } from './cc';
import { enum_ } from '../../model';

export class ProgressBarFilledConverter extends ConverterBase {
    public static prefix = 'pbf-';

    public async convert() {
        const node = this.createNode();
        const pb: any = this.appendComponent(ProgressBar, node);
        pb.progress = 0.5;
        pb.mode = ProgressBar.Mode.FILLED;
        pb.reverse = true;
        const children = this.treePSD.children();
        for (const r of children) {
            if (r.get<string>(enum_.Psd.name).includes('.')) {
                pb.totalLength = r.get<number>(enum_.Psd.width);
                await this.appendSprite(r, node);
            } else {
                const childNode = await this.factory.build(r).convert();
                childNode.name = 'p-bar';
                childNode.parent = node;
                const sprite = childNode.getComponent(
                    ConverterBase.getCcPropFunc(Sprite.name),
                );

                sprite.sizeMode = Sprite.SizeMode.TRIMMED;
                sprite.type = Sprite.Type.FILLED;
                sprite.fillType = Sprite.FillType.RADIAL;
                sprite.fillCenter = ConverterBase.getCcPropFunc('v2')(0.5, 0);
                sprite.fillStart = 1;
                sprite.fillRange = 1;
                pb.barSprite = sprite;
            }
        }

        if (pb.barSprite) {
            pb.barSprite.node.position.y = 0;
        }

        return node;
    }
}